// var fatherEle=document.getElementById("reg")
// console.log(fatherEle);

// fatherEle.onfocus=function(e){
//   console.log(111)
//   var elem=e.target;
//   console.log(elem)
//   if(elem.nodeName=="INPUT"){
//     elem.nextElementSibling.style.display="block";
//   }
// } 不能给父级div加onfocus事件



// 姓名
function show1(){
  tname.nextElementSibling.style.display="block";
}
function test1(){
  var rrreg=/^[\u4E00-\u9FA5]{2,4}$/;
  if(rrreg.test(tname.value)){
    tname.nextElementSibling.style.color="green";
    tname.nextElementSibling.innerHTML="用户名可以使用"
  }else{
    tname.nextElementSibling.style.color="red";
    tname.nextElementSibling.innerHTML="请输入正确格式用户名";
    return;
  }
}

// 密码
function show2(){
  tpwd.nextElementSibling.style.display="block";
}
function test2(){
  var rrreg=/^(\w){6,20}$/;
  if(rrreg.test(tpwd.value)){
    tpwd.nextElementSibling.style.color="green";
    tpwd.nextElementSibling.innerHTML="密码可以使用"
  }else{
    tpwd.nextElementSibling.style.color="red";
    tpwd.nextElementSibling.innerHTML="请输入正确格式密码"
  }
}

// 密码验证
function show3(){
  tpwdTest.nextElementSibling.style.display="block";
}
function test3(){
  if(tpwd.value==tpwdTest.value){
    tpwdTest.nextElementSibling.style.color="green";
    tpwdTest.nextElementSibling.innerHTML="密码相同，可以使用"   
  }else{
    tpwdTest.nextElementSibling.style.color="red";
    tpwdTest.nextElementSibling.innerHTML="两次输入密码不容"  
  }
}

//手机号
function show4(){
  tphone.nextElementSibling.style.display="block";
}
function test4(){
  var rrreg=/^1[3-8]\d{9}$/;
  if(rrreg.test(tphone.value)){
    tphone.nextElementSibling.style.color="green";
    tphone.nextElementSibling.innerHTML="手机号可以使用"
  }else{
    tphone.nextElementSibling.style.color="red";
    tphone.nextElementSibling.innerHTML="请输入正确格式手机号"
  }
}


var tname=document.getElementById("uname");

var tpwd=document.getElementById("upwd");

var tpwdTest=document.getElementById("upwdTest");

var tphone=document.getElementById("uphone");

function reg(){
    //发送ajax请求
    var haveRead=document.getElementById("haveRead");
    if(haveRead.checked==true){
      var uname=tname.value;
      var upwd=tpwd.value;
      var upwdTest=tpwdTest.value;
      var uphone=tphone.value;
      if(! /^[\u4E00-\u9FA5]{2,4}$/.test(uname) || !/^(\w){6,20}$/.test(upwd) || !upwd==upwdTest || !/^1[3-8]\d{9}$/.test(uphone)){
        alert("请正确填写上述信息");
        return;
      }
      //console.log(uid,uname,upwd,upwdTexst,uphone,uemail)
      var formdata="uname="+uname+"&upwd="+upwd+"&uphone="+uphone;
      //console.log(formdata);
      ajax({
        url:"/user/reg",
        type:"post",
        data:formdata,
        dataType:"json" //自动JSON.parse()
      })//return Promise
        //  onreadystatechange中: 
        //           result <- 收到服务端的
        //  然后把result放入
        //              ↓
        //      open(result)
        //              ↓
      .then(function(result){
        //console.log(result);
        if(result==200)
        alert("注册成功,请前往登录页面登录");
        location.href="login.html";
      })
    }else{
      alert("请先阅读用户注册协议")
    }
  }
